// pages/account/account.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgPath:app.globalData.imgPath,
      account:0,
      num:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    //获取个人信息
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          self.setData({ account: data.data.balance })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputnum:function(e){
    this.setData({num:e.detail.value})
  },
  confrim:function(){
    let num=this.data.num;
    let self=this;
    if(num<=0){
      wx.showToast({
        title: '请输入正确金额',
        icon:'none'
      })
      return false;
    }
    wx.request({
      url: app.globalData.reqPath + 'shop/RechargePay',
      data: {
        amount: num,
        payType: 5
      },
      header: {
        'authorization': app.globalData.authorization
      },
      method: 'POST',
      success: function ({ data }) {
        if (data.code == 200) {
          let paydata = data.data;
          wx.requestPayment({
            timeStamp: paydata.timeStamp,
            nonceStr: paydata.nonceStr,
            package: paydata.package,
            signType: paydata.signType,
            paySign: paydata.paySign,
            success(res) {
              console.log(res)
              if (res.errMsg == "requestPayment:ok") {
                wx.redirectTo({
                  url: '/pages/balancelist/balancelist',
                })
              }
            },
            fail(res) {
              if (res.errMsg == 'requestPayment:fail cancel') {//res.errMsg //requestPayment: fail cancel //用户取消支付
                self.onLoad();
              } else { //requestPayment: fail(detail message)//调用支付失败
                wx.showToast({
                  title: '支付失败，请重试',
                  icon: 'none'
                })
              }
            }
          })
        }else{

        }
      }
    })
  }
})